<?php

namespace App\Service\CommodityService;

use App\Service\BaseService;
use App\Service\Code\Normal;
use Hyperf\Task\Annotation\Task;
use App\Service\InterfaceService\SupplierVendorApiGoodsServiceInterface;
use App\Model\CommodityModel\SupplierCompany;
use App\Model\CommodityModel\WarehouseInfo;

class SupplierVendorApiGoodsService extends BaseService implements SupplierVendorApiGoodsServiceInterface
{
    //供应商路由(下单、获取商品、取消、售后、发货物流)
    protected $supplierRouterPath = "App\\Service\\OrderService\\OrderRouteService\\";
    
    /**
     * 获取第三方对接商品接口列表数据
     * @param array $param
     * $params['supplierID']                = (string) 供应商ID (Required)
     * $params['page']                      = (int) 页码（从1开始）
     * $params['limit']                     = (int) 每页大小
     * Task
     */
    public function queryGoodsList( $param = [] )
    {
        $result = $this->checkSupplierParameters($param);
        if( $result['code'] != Normal::SUC ) {
            return $result;
        }
        $data = $result['data'];
        $classPath  = $this->supplierRouterPath . $data['supplierCode'];
        return container()->get($classPath)->{__FUNCTION__}($data);
    }
    
    /**
     * 获取供应商商品详情
     * @param array $param
     * $params['supplierID']             = (string) 供应商ID (Required)
     * $params['supplierSkuCode']        = (string) 供应商商品sku
     * @Task
     */
    public function queryGoodsDetail( $param = [] )
    {
        $result = $this->checkSupplierParameters($param);
        if( $result['code'] != Normal::SUC ) {
            return $result;
        }
        
        $data = $result['data'];
        $classPath  = $this->supplierRouterPath . $data['supplierCode'];
        return container()->get($classPath)->{__FUNCTION__}($data);
    }
    
    /**
     * 获取供应商订单物流信息
     * @param array $param
     * $params['supplierID']    = (string) 供应商ID (Required)
     * $params['orderID']       = (string) 订单ID
     * @Task
     */
    public function queryOrderExpress( $param = [] )
    {
        $result = $this->checkSupplierParameters($param);
        if( $result['code'] != Normal::SUC ) {
            return $result;
        }
        
        if( empty($param['orderID']) ) {
            return Normal::returnCode(Normal::FAIL,"orderID不能为空");
        }
        
        $data = $result['data'];
        $classPath  = $this->supplierRouterPath . $data['supplierCode'];
        return container()->get($classPath)->{__FUNCTION__}($data);
    }
    
    /**
     * 查询供应商售后信息
     * @param array $param
     * $params['supplierID']    = (string) 供应商ID (Required)
     * $params['orderID']       = (string) 订单ID
     * @Task
     */
    public function queryOrderAfterSale( $param = [] )
    {
        $result = $this->checkSupplierParameters($param);
        if( $result['code'] != Normal::SUC ) {
            return $result;
        }
        
        if( empty($param['orderID']) ) {
            return Normal::returnCode(Normal::FAIL,"orderID不能为空");
        }
        
        $data = $result['data'];
        $classPath  = $this->supplierRouterPath . $data['supplierCode'];
        return container()->get($classPath)->{__FUNCTION__}($data);
    }

    /**
     * 获取物流轨迹信息
     * @param array $param
     * $params['supplierID']    = (string) 供应商ID (Required)
     * $params['expressNo']       = (string) 物流单号
     * @Task
     */
    public function queryExpressLogistics( $param = [] )
    {
        $result = $this->checkSupplierParameters($param);
        if( $result['code'] != Normal::SUC ) {
            return $result;
        }

        $data = $result['data'];
        $classPath  = $this->supplierRouterPath . $data['supplierCode'];
        return container()->get($classPath)->{__FUNCTION__}($data);
    }

    /**
     * 获取订单详情信息
     * @param array $param
     * $params['supplierID']    = (string) 供应商ID (Required)
     * $params['orderSn']       = (string) 订单单号
     * @Task
     */
    public function queryOrderDetail( $param = [] )
    {
        $result = $this->checkSupplierParameters($param);
        if( $result['code'] != Normal::SUC ) {
            return $result;
        }

        $data = $result['data'];
        $classPath  = $this->supplierRouterPath . $data['supplierCode'];
        return container()->get($classPath)->{__FUNCTION__}($data);
    }

    /**
     * 获取订单异步创建并且获取物流信息结果
     * @param array $param
     * $params['supplierID']    = (string) 供应商ID (Required)
     * $params['orderSn']       = (string) 订单单号
     * @Task
     */
    public function queryOrderAsyncExpressNotify( $param = [] )
    {
        $result = $this->checkSupplierParameters($param);
        if( $result['code'] != Normal::SUC ) {
            return $result;
        }

        $data = $result['data'];
        $classPath  = $this->supplierRouterPath . $data['supplierCode'];
        return $classPath::instance()->get($classPath)->{__FUNCTION__}($data);
    }

    /**
     * 校验供应商参数信息
     * @param array $supplierRow
     */
    protected function checkSupplierParameters( $param = [] )
    {
        if( empty($param['supplierID']) ) {
            return Normal::returnCode(Normal::FAIL,"except param supplierID");
        }
        
        $supplier = SupplierCompany::instance()->info(['ID'=>(string)$param['supplierID'],'CompanyStatus'=>1,'OrderDockMode'=>0]);
        if( empty($supplier) ) {
            return Normal::returnCode(Normal::FAIL,"供应商信息不存在或者被禁用，无法拉取查询商品!");
        }
        
        $wareHouse = WarehouseInfo::instance()->info(['SupplierID'=>(string)$param['supplierID'],'ExamineState'=>1]);
        if( empty($wareHouse) ) {
            return Normal::returnCode(Normal::FAIL,"供应商仓库信息不存在或者被禁用，无法拉取查询商品!");
        }
        
        $jsonBody = @json_decode($wareHouse['JsonBody'],true);
        $jsonBody = $jsonBody ? $jsonBody : [];
        $supplierCode = ucfirst(strtolower( $supplier['ThirdPartyPlatformCode'] ? $supplier['ThirdPartyPlatformCode'] : $supplier['SupplierCode'] ));
        return Normal::returnCode(Normal::SUC,"校验通过",[
            'supplierCode'  => $supplierCode,
            'apiBody'       => $jsonBody,
            'queryBody'     => $param
        ]);
    }
}